package wgota

import (
	"ac"
	log "ac-common-go/glog"
	"ac-julink/rpcagent"
	"ac-julink/tool/config"
	"git.apache.org/thrift.git/lib/go/thrift"
	"net"
	"strconv"
	"time"
)

var thriftPoolManager *rpcagent.ThriftPoolManager

func dial(ip, port string, connTimeout time.Duration) (*rpcagent.IdleClient, error) {
	tSocket, err := thrift.NewTSocketTimeout(net.JoinHostPort(ip, port), connTimeout)
	if err != nil {
		log.Errorf("connect to wgota error")
		return nil, err
	}
	if err := tSocket.Open(); err != nil {
		return nil, err
	}
	transport := thrift.NewStreamTransportFactory(tSocket, tSocket, true).GetTransport(tSocket)
	//	transport := thrift.NewTTransportFactory().GetTransport(tSocket)
	//	transport := thrift.NewTBufferedTransportFactory(1024).GetTransport(tSocket)
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	client := NewWgotaAPIClientFactory(transport, protocolFactory)
	return &rpcagent.IdleClient{Socket: tSocket, Client: client, CreateTime: time.Now()}, nil
}
func close(c *rpcagent.IdleClient) error {
	err := c.Socket.Close()
	return err
}

var httpProxy *rpcagent.Proxy

func InitTriftPool() {
	ip := config.GetConfigValue("WGNS", "http_ota_ip")
	if ip == "" {
		log.Errorf("http_ota_ip is not valid!!")
		return
	}
	httpPort := config.GetConfigValue("WGNS", "http_ota_port")
	if httpPort == "" {
		log.Errorf("http_ota_port is not valid!!")
		return
	}

	maxConnsPerHost, ok := config.GetConfigIntValue("WGNS", "http_ota_maxconnsPerHost")
	if !ok {
		maxConnsPerHost = rpcagent.DEFAULTMAXCONNSPERHOST
	}

	respHeaderTimeout, ok := config.GetConfigIntValue("WGNS", "http_ota_response_header_timeout")
	if !ok {
		respHeaderTimeout = rpcagent.DEFAULTRESPHEASERTIMEOUT
	}
	idleTimeout, ok := config.GetConfigIntValue("WGNS", "http_ota_idletimeout")
	if !ok {
		idleTimeout = rpcagent.DEFAULTIDLETIME
	}
	retryNum, ok := config.GetConfigIntValue("WGNS", "http_ota_retrynum")
	if !ok {
		retryNum = rpcagent.DEFAULTRETRYNUM
	}

	httpProxy = rpcagent.NewProxy(ip, httpPort, maxConnsPerHost, respHeaderTimeout, idleTimeout, retryNum, false)
	log.Infof("CTL http pool init success!")
}

/*
func WgotaCheckVerReq(wgota_check_ver_req *InWgotaCheckVerReqT) (r *OutWgotaCheckVerReqT, err error) {
	log.Infof("Rpc call WgotaCheckVerReq ==>>%s", wgota_check_ver_req.GetDvcID())
	if thriftPoolManager == nil {
		return nil, errors.New("wgota pool manager is nil")
	}
	args := make([]interface{}, 1)
	args[0] = wgota_check_ver_req
	result, err := thriftPoolManager.AskRpc("WgotaCheckVerReq", args)
	if err != nil {
		return nil, err
	}
	b, ok := result.(*OutWgotaCheckVerReqT)
	if !ok {
		return nil, errors.New("interface to OutWgotaCheckVerReqT failed")
	}
	return b, nil
}*/
//xiaohong add
type HttpCommonResp struct {
	ResultCode   int32  `json:"resultCode"`
	ErrorCode    int32  `json:"errorCode"`
	ErrorDesc    string `json:"errorDesc"`
	WifiVersion  string `json:"wifiVersion"`
	Url          string `json:"url"`
	FileSize     int32  `json:"fileSize"`
	Sign         string `json:"sign"`
	UpdateFlag   int32  `json:"updateFlag"`
	VersionDesc  string `json:"versionDesc"`
	PolicyId     int64  `json:"policyId"`
	TargetedType int64  `json:"targetedType"`
}

type Wgota struct {
	WifiId string `json:"wifiId"`
}

func WgotaCheckVerReq(_wifiid, _wifiVer string, languagId int32) (r *HttpCommonResp, err error) {
	log.Infof("Http call WgotaCheckVerReq ")
	reqctx := ac.NewZContext("", "", 0, 0, "", "zc-julink")

	var resp HttpCommonResp
	if err := httpProxy.DirectCall(reqctx, "device/check_wifi_device_version", "GET", "wifiId="+_wifiid+"&originalVersion="+_wifiVer+"&languageId="+strconv.Itoa(int(languagId)), nil, &resp); err != nil {
		log.Warningf("call reportDeviceCmdresult: err[%v]", err)
		return nil, err
	}
	return &resp, nil
}

//xiaohong add end
